/**
 * 
 */
package cn.biplam.back.sql.dml;

import java.util.Map;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;

/**
 * Update语义模型
 * 简单模式用法：<BR>
 * <P>new Update("表物理名称");</P>
 * <P>this.put(String 字段物理名称,String 字段值[包括必要的单引号]);</P>
 * <P>this.setWhere(String 过滤表达式);[可选]</P>
 * <P>String this.getSimpleSql();</P>
 * 
 * @author 	WangSong 2009-6-11 上午11:21:11
 * @version 1.0
 */
public class Update extends DmlBase {
	
	private KvMap<String, String> kv;
	private String sWhere;

	
	public Update( String tableName) {
		super("Update", tableName);
		this.kv=new KvMap<String, String>();
	}
	
	/**
	 * 直接成对传入Update语句的字段和值(如果需要,值的单引号在外部创建好)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage 
	 */
	public void putField(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Update disallow put Simple fileds kv.");
		}
		this.kv.put(sFieldName, sFieldValue);
	}
	
	/**
	 * 直接成对传入Update语句的字段和值(如果需要,值的单引号在外部创建好)
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage 
	 */
	public void putStringField(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Update disallow put Simple fileds kv.");
		}
		this.kv.put(sFieldName, "'"+sFieldValue+"'");
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果值为空则忽略该字段
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putFieldIgnoreNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsNull(sFieldValue)){
			if(TOOLS.StringIsEmpty(sFieldValue)){
				this.putField(sFieldName, null);
			}else{
				this.putField(sFieldName, sFieldValue);
			}
		}
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果字段值为空,抛出异常
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putFieldDenyNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(TOOLS.StringIsEmpty(sFieldValue)){
			throw new ErrorMessage("Insert fields value is empty:"+sFieldName+".");
		}else{
			this.putField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果值为空则忽略该字段
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldIgnoreNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsNull(sFieldValue)){
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果值为null忽略该字段,empty不忽略
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldIgnoreNullAndIgnoreEmpty(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(!TOOLS.StringIsEmpty(sFieldValue)){
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 直接传入Insert语句的字段和值,如果字段值为空,抛出异常
	 * @param sFieldName
	 * @param sFieldValue
	 * @throws ErrorMessage
	 */
	public void putStringFieldDenyNull(String sFieldName,String sFieldValue) throws ErrorMessage{
		if(TOOLS.StringIsEmpty(sFieldValue)){
			throw new ErrorMessage("Insert fields value is empty:"+sFieldName+".");
		}else{
			sFieldValue = TOOLS.stringToDbDeal(sFieldValue);
			this.putStringField(sFieldName, sFieldValue);
		}
	}
	
	/**
	 * 设置简单Update语句的Where条件
	 * @param where
	 * @throws ErrorMessage 
	 */
	public void setWhere(String where) throws ErrorMessage{
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Update disallow put where String.");
		}
		this.sWhere=where;
	}

	/**
	 * 由参数信息直接生成通用DML语句,忽略数据库版本
	 * @return
	 * @throws ErrorMessage 
	 */
	@Override
	public String getSimpleSql() throws ErrorMessage {
		if(this.dmlType==Dml_Type.Complex){
			throw new ErrorMessage("Complex Insert cannot get simple sql.");
		}
		StringBuilder sb = new StringBuilder();
		sb.append("update ");
		sb.append(this.sTableName);
		sb.append(" set ");
		for(Map.Entry<String, String> ite:this.kv){
			sb.append(ite.getKey());
			sb.append("=");
			sb.append(ite.getValue());
			sb.append(',');
		}
		sb.delete(sb.length()-1,sb.length());
		if(this.sWhere!=null){
			sb.append(" where ");
			sb.append(this.sWhere);
		}
		
		return sb.toString();
	}
	
}
